
function rest(parmes, url, type) {

    return new Promise(function (resolve, reject) {
        ajax({
            url: url,
            type: type,
            data: {
                ...parmes
            },
            dataType: "json",
            success: function (result) {
                resolve(result)
            }
        })
    })
}

function isExist(parmes) {
    return rest(parmes, "../php/isExist.php", "get");

}
function register(parmes) {
    return rest(parmes, "../php/register.php", "post");

}
function login(parmes) {
    return rest(parmes, "../php/sgin.php", "post");
}
function searchGoodsOrderLimit(parmes) {
    return rest(parmes, "../php/searchGoodsOrderLimit.php", "get");
}
function searchGoodsById(parmes) {
    return rest(parmes, "../php/searchGoodsById.php", "get")
}
function addToShoppingCar(parmes) {
    return rest(parmes, "../php/addToShoppingCar.php", "post");
}
function searchShoppingCarByUser(parmes) {
    return rest(parmes, "../php/searchShoppingCarByUser.php", "post");
}
function deleteShoppingCarById(parmes) {
    return rest(parmes, "../php/deleteShoppingCarById.php", "get");
}
function updateGradeById(parmes) {
    return rest(parmes, "../php/updateGoodsById.php", "get")
}
// let searchGoodsOrderLimit = parmes => rest(parmes, "../php/searchGoodsOrderLimit.php", "get");
// let searchGoodsById = parmes => rest(parmes, "../php/searchGoodsById.php", "get")
// let addToShoppingCar = parmes => rest(parmes, "../php/addToShoppingCar.php", "post");
// let searchShoppingCarByUser = parmes => rest(parmes, "../php/searchShoppingCarByUser.php", "post");
// let deleteShoppingCarById = parmes => rest(parmes, "../php/deleteShoppingCarById.php", "get");
// let updateGradeById = parmes => rest(parmes, "../php/updateGoodsById.php", "get")


function ajax(options) {
    var { type = "get", url, data = "", async = "true", dataType = "text", success } = options;
    if (typeof data == "object") {
        var arr = [];
        for (var key in data) {
            var val = data[key];
            arr.push(key + "=" + val);
        }
        data = arr.join("&");
        // console.log(data);
    }
    var xhr = new XMLHttpRequest();
    if (type.toLowerCase() == "post") {
        xhr.open(type, url, async);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(data);
    } else if (type.toLowerCase() == "get") {
        xhr.open(type, data ? url + "?" + data : url, async);
        xhr.send();
    }

    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var result = xhr.responseText;
            if (dataType.toLowerCase() == "json") {
                result = JSON.parse(result);
            }
            if (success) {
                success(result);
            }
        }
    }
}